package com.incar.chuxing.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.widget.TextView;

import com.incar.chuxing.R;
import com.incar.chuxing.adapter.QualifyPagerAdapter;
import com.incar.chuxing.base.BaseActivity;
import com.incar.chuxing.fragment.DepositCarFragment;
import com.incar.chuxing.fragment.LicenseFragment;
import com.incar.chuxing.fragment.RealNameFragment;
import com.incar.chuxing.utils.AppManager;
import com.incar.chuxing.utils.SharedPreferencesKey;
import com.incar.chuxing.widget.NoScrollViewPager;
import com.incar.chuxing.utils.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class QualificationActivity extends BaseActivity {
    @BindView(R.id.fragment_vp)
    NoScrollViewPager fragmentVp;
    @BindView(R.id.title_name)
    public TextView tvTitle;
    private List<Fragment> totalList;
    private Context mContext = this;
    public NoScrollViewPager mVp;
    private QualifyPagerAdapter adapter;
    private String identityStatus;
    private String licenseStatus;
    private String depositStatus;

    @Override
    public int getLayoutId() {
        return R.layout.activity_certificate;
    }

    @Override
    public void initView() {
    }

    @Override
    public void initData() {
        // 碎片经理和事务
        // 将碎片添加进集合中
        mVp = (NoScrollViewPager) findViewById(R.id.fragment_vp);
        mVp.setNoScroll(true);
        totalList = new ArrayList<>();
        totalList.add(new DepositCarFragment());
        totalList.add(new RealNameFragment());
        totalList.add(new LicenseFragment());
        adapter = new QualifyPagerAdapter(getSupportFragmentManager(), totalList);
        mVp.setAdapter(adapter);
        identityStatus = SharedPreferencesUtils.getInstance(mContext).getStringValue(SharedPreferencesKey.REAL_NAME_STATUS);
        licenseStatus = SharedPreferencesUtils.getInstance(mContext).getStringValue(SharedPreferencesKey.LICENSE_CAR_STATUS);
        depositStatus = SharedPreferencesUtils.getInstance(mContext).getStringValue(SharedPreferencesKey.DEPOSIT_CAR_STATUS);
        mVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                Log.i("TAGn", position + "");
                if (position == 1) {
                    tvTitle.setText("实名认证");
                } else if (position == 2) {
                    tvTitle.setText("证照核验");
                } else if (position == 0) {
                    tvTitle.setText("缴保证金");
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });


        Intent intent = getIntent();
        int flag = intent.getIntExtra("SubmitFlag", -1);

        switch (flag) {
            case -1:
                switch (depositStatus) {
                    case "0":
                        tvTitle.setText("缴保证金");
                        mVp.setCurrentItem(0);
                        break;
                    case "1":
                        switch (identityStatus) {
                            //身份证审核中
                            //身份证审核成功
                            case "11":
                            case "13":
                                switch (licenseStatus) {
                                    //驾驶证未审核
                                    //驾驶证失败
                                    case "10":
                                    case "12":
                                        mVp.setCurrentItem(2);
                                        break;
                                    //驾驶证审核中
                                    //驾驶证审核成功
                                    case "11":
                                    case "13":
                                        AppManager.getAppManager().finishActivity();
                                        break;
                                }
                                break;
                            //身份证未审核
                            //身份证失败
                            case "10":
                            case "12":
                                mVp.setCurrentItem(1);
                                break;
                        }
                        break;
                }
                break;
            //实名认证
            case 1:
                mVp.setCurrentItem(1);
                break;
            //证照核验
            case 2:
                mVp.setCurrentItem(2);
                break;
            //保证金
            case 3:
                tvTitle.setText("缴保证金");
                mVp.setCurrentItem(0);
                break;
        }
    }

    @OnClick(R.id.title_back)
    public void onClick() {
        AppManager.getAppManager().finishActivity();
    }

    @Override
    public void onBackPressed() {
        AppManager.getAppManager().finishActivity();
    }
}
